package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Componente;
import com.lostreseditores.sabersai.model.dto.ComponenteDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class ComponenteView {
    private InputText txtCompciadescript;
    private InputText txtCompciasiglav;
    private InputText txtCompciaidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<ComponenteDTO> data;
    private ComponenteDTO selectedComponente;

    public ComponenteView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            ComponenteDTO componenteDTO = (ComponenteDTO) e.getObject();

            if (txtCompciadescript == null) {
                txtCompciadescript = new InputText();
            }

            txtCompciadescript.setValue(componenteDTO.getCompciadescript());

            if (txtCompciasiglav == null) {
                txtCompciasiglav = new InputText();
            }

            txtCompciasiglav.setValue(componenteDTO.getCompciasiglav());

            if (txtCompciaidn == null) {
                txtCompciaidn = new InputText();
            }

            txtCompciaidn.setValue(componenteDTO.getCompciaidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtCompciadescript != null) {
            txtCompciadescript.setValue(null);
            txtCompciadescript.setDisabled(true);
        }

        if (txtCompciasiglav != null) {
            txtCompciasiglav.setValue(null);
            txtCompciasiglav.setDisabled(true);
        }

        if (txtCompciaidn != null) {
            txtCompciaidn.setValue(null);
            txtCompciaidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Componente entity = null;

        try {
            Integer compciaidn = new Integer(txtCompciaidn.getValue().toString());
            entity = BusinessDelegatorView.getComponente(compciaidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtCompciadescript.setDisabled(false);
            txtCompciasiglav.setDisabled(false);
            txtCompciaidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtCompciadescript.setValue(entity.getCompciadescript());
            txtCompciadescript.setDisabled(false);
            txtCompciasiglav.setValue(entity.getCompciasiglav());
            txtCompciasiglav.setDisabled(false);
            txtCompciaidn.setValue(entity.getCompciaidn());
            txtCompciaidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveComponente(FacesUtils.checkString(
                    txtCompciadescript),
                FacesUtils.checkInteger(txtCompciaidn),
                FacesUtils.checkString(txtCompciasiglav));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteComponente(FacesUtils.checkInteger(
                    txtCompciaidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateComponente(FacesUtils.checkString(
                    txtCompciadescript),
                FacesUtils.checkInteger(txtCompciaidn),
                FacesUtils.checkString(txtCompciasiglav));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtCompciaidn == null) {
                txtCompciaidn = new InputText();
            }

            txtCompciaidn.setValue(selectedComponente.getCompciaidn());

            BusinessDelegatorView.deleteComponente(FacesUtils.checkInteger(
                    txtCompciaidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedComponente);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String compciadescript,
        Integer compciaidn, String compciasiglav) throws Exception {
        try {
            BusinessDelegatorView.updateComponente(compciadescript, compciaidn,
                compciasiglav);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("ComponenteView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtCompciadescript() {
        return txtCompciadescript;
    }

    public void setTxtCompciadescript(InputText txtCompciadescript) {
        this.txtCompciadescript = txtCompciadescript;
    }

    public InputText getTxtCompciasiglav() {
        return txtCompciasiglav;
    }

    public void setTxtCompciasiglav(InputText txtCompciasiglav) {
        this.txtCompciasiglav = txtCompciasiglav;
    }

    public InputText getTxtCompciaidn() {
        return txtCompciaidn;
    }

    public void setTxtCompciaidn(InputText txtCompciaidn) {
        this.txtCompciaidn = txtCompciaidn;
    }

    public List<ComponenteDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataComponente();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<ComponenteDTO> componenteDTO) {
        this.data = componenteDTO;
    }

    public ComponenteDTO getSelectedComponente() {
        return selectedComponente;
    }

    public void setSelectedComponente(ComponenteDTO componente) {
        this.selectedComponente = componente;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
